package com.example.chaptermenu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = this.getMenuInflater();
        inflater.inflate(R.menu.mymenu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){

            case R.id.menu_settings:
                Toast.makeText(this, "你点击了 设置", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.menu_document:
                Toast.makeText(this, "你点击了联机文档", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.menu_save:
                item.setChecked(!item.isChecked());
                String save = "关";
                if (item.isChecked()){
                    save= "开";
                }

                Toast.makeText(this, "您点击了菜单项：自动保存  "+save, Toast.LENGTH_SHORT).show();
                return true;
            case R.id.menu_search:
                Toast.makeText(this, "你点击了 搜索", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.menu_about:
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setTitle("关于我们");
                builder.setMessage("这是教材的示例程序！\\n版本号：3.0");
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                }).show();
                return true;
            case R.id.menu_quit:
                MainActivity.this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}